package com.example.expresshelp.service;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.example.expresshelp.bean.Address;
import com.example.expresshelp.bean.City;
import com.example.expresshelp.bean.District;
import com.example.expresshelp.bean.Province;
import com.example.expresshelp.bean.Towntr;
import com.example.expresshelp.bean.Villagetr;
import com.example.expresshelp.db.MySqliteHelper;
import com.example.expresshelp.util.Constance;
import com.example.expresshelp.util.HttpClientHelper;
import com.example.expresshelp.util.Validator;

/**
 * 村
 * 
 * @author josan_tang
 * 
 */
public class VillagetrService {
	private static final String TAG = "VillagetrService";

	/**
	 * 解析信息
	 * 
	 */
	public static List<Villagetr> parseVillagetr(Context context, String pCode,
			String cCode, String dCode, String tid, String tCode, String html) {
		List<Villagetr> villagetrList = new ArrayList<Villagetr>();
		String resultHtml = HttpClientHelper.doPost(html, null, 0);
		if (!Validator.isEffective(resultHtml)) {
			return null;
		}
		Document document = Jsoup.parse(resultHtml);
		Elements districtRows = document.getElementsByClass("villagetr");

		for (int i = 0; i < districtRows.size(); i++) {
			Villagetr villagetr = new Villagetr();
			Element tr = districtRows.get(i);
			Elements tds = tr.getElementsByTag("td");
			// 第一个td里是超链接没和城市代码
			Element codeTd = tds.get(0);
			String villagetrCode = codeTd.text().substring(9, 12);
			// 第三个td里面有名称
			Element nameTd = tds.get(2);
			String villagetrName = nameTd.text();
			Log.i("express", "villagetr - >" + villagetrCode + villagetrName);
			long vid = saveToDB(context, tid, villagetrCode, villagetrName);
			villagetr.setId(vid + "");
			villagetr.setTid(tid);
			villagetr.setTowntrCode(tCode);
			villagetr.setCode(villagetrCode);
			villagetr.setName(villagetrName);
			villagetrList.add(villagetr);
		}
		return villagetrList;
	}

	/**
	 * 把信息保存到数据库中
	 * 
	 * @param context
	 * @param code
	 * @param name
	 */
	public static long saveToDB(Context context, String tCode,
			String villagetrCode, String villagetrName) {
		try {
			// 构造参数
			ContentValues values = new ContentValues();
			values.put("tcode", tCode);
			values.put("code", villagetrCode);
			values.put("name", villagetrName);
			return MySqliteHelper.getInstance(context).insert(
					Constance.VILLAGETR_TABLE_NMAE, null, values);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 
	 * @param context
	 * @param code
	 * @return
	 */
	public static List<Villagetr> getVillagetrByTowntrcode(Context context,
			String towntrCode) {
		List<Villagetr> list = null;
		String where = " tcode=" + towntrCode;
		Cursor cursor = MySqliteHelper.getInstance(context).query(
				Constance.VILLAGETR_TABLE_NMAE, where);
		if (cursor != null) {
			list = new ArrayList<Villagetr>();
			while (cursor.moveToNext()) {
				Villagetr villagetr = new Villagetr();
				villagetr.setTowntrCode(towntrCode);
				villagetr.setCode(cursor.getString(cursor
						.getColumnIndex("code")));
				villagetr.setName(cursor.getString(cursor
						.getColumnIndex("name")));
				list.add(villagetr);
			}
		}
		return list;
	}

	/**
	 * 
	 * @param context
	 * @param code
	 * @return
	 */
	public static List<Address> getVillagetrByName(Context context, String name) {
		List<Address> addressList = null;
		List<Villagetr> list = null;
		String where = " name like '%" + name + "%'";
		Cursor cursor = MySqliteHelper.getInstance(context).query(
				Constance.VILLAGETR_TABLE_NMAE, where);
		if (cursor != null) {
			list = new ArrayList<Villagetr>();
			addressList = new ArrayList<Address>();
			while (cursor.moveToNext()) {
				Villagetr villagetr = new Villagetr();
				Address address = new Address();
				StringBuffer totalAddress = new StringBuffer();
				String tcode = cursor.getString(cursor.getColumnIndex("tcode"));
				villagetr.setTowntrCode(tcode);
				villagetr.setCode(cursor.getString(cursor
						.getColumnIndex("code")));
				villagetr.setName(cursor.getString(cursor
						.getColumnIndex("name")));
				list.add(villagetr);
				address.setFilterAddress(villagetr.getName());
				Towntr towntr = TowntrService.find(context, tcode);
				if (towntr != null) {
					totalAddress.append(",");
					totalAddress.append(towntr.getName());
					District district = DistrictService.find(context,
							towntr.getDid());
					if (district != null) {
						totalAddress.append(",");
						totalAddress.append(district.getName());
						City city = CityService
								.find(context, district.getCid());
						if (city != null) {
							String cityName = city.getName();
							if (!(cityName.equals("市辖区") || cityName.equals("县"))) {
								totalAddress.append(",");
								totalAddress.append(city.getName());
							}
							Province province = ProvinceService.find(context, city.getPid());
							if (province != null) {
								totalAddress.append(",");
								totalAddress.append(province.getName());
							}
						}
					}
				}
				address.setUnfilterAddress(totalAddress.toString());
				addressList.add(address);
			}
		}
		return addressList;
	}
}
